<template>
  <div class="page-login">
    登录
    <div class="black-bg-cover"></div>
    <el-form class="login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm"
             label-position="left">
      <div class="tc pb20">
        <img src="../assets/imgs/logo_login.png" width="160" height="160" alt="天星logo">
      </div>
      <div class="title">未来脑 - 组卷在线<small>v1.0</small></div>
      <el-form-item class="item-input" :class="{'focus': focusUserName}" prop="username">
        <span class="icon-box">
          <i class="iconfont tx-icon-seeuser"></i>
        </span>
        <el-input class="bg-input" name="username"
                  type="text" auto-complete="on"
                  @focus="focusUserName = true"
                  @blur="focusUserName = false"
                  v-model.trim="loginForm.username"
                  placeholder="请输入帐号"></el-input>
      </el-form-item>

      <el-form-item class="item-input" :class="{'focus': focusPwd}" prop="password">
        <span class="icon-box">
          <i class="tx-icon-lock iconfont"></i>
        </span>
        <el-input class="bg-input" name="password" auto-complete="on" :type="pwdType"
                  @keyup.native.enter="bindLogin"
                  v-model.trim="loginForm.password"
                  @focus="focusPwd = true"
                  @blur="focusPwd = false"
                  placeholder="请输入密码">
        </el-input>
        <span class="show-pwd" @click="showPwd"><i class="el-icon-view"></i></span>
      </el-form-item>

      <!--验证码-->
      <!--<el-form-item prop="code">-->
      <!--<el-input placeholder="请输入内容" v-model="loginForm.captcha">-->
      <!--<el-button slot="append">获取验证码</el-button>-->
      <!--</el-input>-->
      <!--</el-form-item>-->

      <el-form-item>
        <el-button size="large" type="primary" style="width:100%; font-size:18px;" :loading="loading"
                   @click.prevent="bindLogin">
          登录
        </el-button>
      </el-form-item>

      <!--<div class="tips">-->
        <!--<span>Copyright © 1998-2018 All Rights Reserved 河南天星教育传媒股份有限公司</span>-->
      <!--</div>-->

      <div class="copyright">
        <span>Copyright © 1998-2018 All Rights Reserved 河南天星教育传媒股份有限公司</span>
      </div>
    </el-form>
  </div>
</template>

<script>
  import { mapActions } from 'vuex'
  import { getCode, signIn } from '../api/login'
  import { BASE_URL } from '../config'

  export default {
    name: 'Login',
    data () {
      // const isvalidUsername = (user) => {
      //   return user === 'admin'
      // }
      // const validateUsername = (rule, value, callback) => {
      //   if (!isvalidUsername(value)) {
      //     callback(new Error('请输入正确的用户名'))
      //   } else {
      //     callback()
      //   }
      // }
      const validatePass = (rule, value, callback) => {
        if (value.length < 5) {
          callback(new Error('密码不能小于5位'))
        } else {
          callback()
        }
      }
      return {
        focusUserName: false,
        focusPwd: false,
        loginForm: {
          mobile: '',
          username: '',
          password: ''
          // captcha: 'usdt'
        },
        loginRules: {
          // , validator: validatePass
          username: [{required: true, message: '请输入帐号', trigger: 'change'}],
          password: [{required: true, message: '请输入密码', trigger: 'change'}]
        },
        loading: false,
        pwdType: 'password'
      }
    },
    created () {
      // this.getCode() // 获取验证码
    },
    methods: {
      ...mapActions('common', ['getAllBaseData']),
      showPwd () {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      getCode () {
        getCode().then(res => {
          console.log(res)
        })
      },
      login () {
        let _self = this
        this.loading = true
        let from = _self.$route.query.from
        let baseUrlRE = new RegExp(`^${BASE_URL}`)
        signIn(_self.loginForm).then((res) => {
          this.getAllBaseData()
          localStorage.setItem('real_name', res.data.real_name || _self.loginForm.username)
          _self.$router.push({path: (from.replace(baseUrlRE, '/') || '/')})
          _self.loading = false
        }).catch(() => {
          _self.loading = false
        })
      },
      bindLogin () {
        let _self = this
        _self.$refs['loginForm'].validate(valid => {
          if (valid) {
            _self.login()
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style lang="less">
  /* reset element-ui css */
  .page-login {
    @bg: #d7e8f9;
    @dark-gray: #d7d7e1;
    @light-gray: #eee;
    position: relative;
    height: 100%;
    width: 100%;
    min-height:600px;
    background-color: @bg;
    .black-bg-cover {
      filter: blur(2px);
      /*background: url('../assets/imgs/login_bg.jpg') center center no-repeat;*/
      background-size: cover;
      position: absolute;
      right: 0;
      top: 0;
      left: 0;
      bottom: 0;
      background-color: rgba(0, 0, 0, .6);
    }
    .login-form {
      position: absolute;
      left: 0;
      right: 0;
      width: 420px;
      /*padding: 0px 35px 15px 35px;*/
      margin: 13vh auto;
    }
    .bg-input {
      display: inline-block;
      height: 47px;
      width: 85%;
      input {
        background: transparent;
        border: 0;
        -webkit-appearance: none;
        border-radius: 0;
        padding: 12px 5px 12px 0;
        color: @light-gray;
        height: 47px;
        &:-webkit-autofill {
          -webkit-box-shadow: 0 0 0 1000px @bg inset !important;
          -webkit-text-fill-color: #fff !important;
        }
        &::-webkit-input-placeholder{
          color: rgba(255,255,255,.5);
        }
      }
    }
    .item-input {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
      box-shadow: 0 0 0 rgba(255, 254, 236, 0.65);
      transition: all .3s;
      &.focus{
        border-color: rgba(255, 255, 255, 0.6);
        box-shadow: 0 0 10px rgba(255, 254, 236, 0.65);
      }
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }
    .copyright {
      font-size: 12px;
      color: rgba(255,255,255,.4);
    }
    .icon-box {
      padding: 6px 10px;
      text-align: center;
      color: @dark-gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .title {
      font-size: 24px;
      color: #fff;
      margin: 0 auto 40px auto;
      text-align: center;
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: @dark-gray;
      cursor: pointer;
      user-select: none;
    }
  }

</style>
